package com.feiyun.api;

import static com.feiyun.exception.ExceptionTypeEnum.SELECT_TIME_OVER_EXCEPTION_TYPE;
import static com.feiyun.util.ResponseUtil.success;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.PositiveOrZero;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyun.common.PageParam;
import com.feiyun.config.WeChatUtil;
import com.feiyun.exception.ServiceException;
import com.feiyun.model.dto.WeChatPhoneDTO;
import com.feiyun.model.entity.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.feiyun.model.dto.ResultVO;
import com.feiyun.model.dto.UserConditionDto;
import com.feiyun.service.ITempUserService;
import com.feiyun.service.IUserService;

/**
 * @ClassName UserController
 * @author: yan
 * @Description: 用户CRUD
 * @time 2023/03/12 17:09:44
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
@Tag(name = "用户CRUD", description = "对用户进行增删改查")
@Slf4j
public class UserController {

    private final IUserService userService;

    private final ITempUserService tempUserService;

    @GetMapping("test")
    @Operation(summary = "test")
    public ResponseEntity<ResultVO<String>> test() {
        throw new ServiceException(SELECT_TIME_OVER_EXCEPTION_TYPE);
    }

    /**
     * 用户登录
     *
     * @param tempUser 临时用户
     * @return ResultVO
     */
    @PostMapping("login")
    @Operation(summary = "用户登录")
    public ResponseEntity<ResultVO<User>> login(
            @Parameter(description = "临时用户实体")
            @RequestBody @Valid TempUser tempUser) {
        log.info("调用用户登录接口,传递参数：{}", JSONObject.toJSONString(tempUser));
        return success(tempUserService.login(tempUser));
    }


    @PostMapping("/wxlogin")
    public ResponseEntity<ResultVO<User>> getPhone(@RequestBody WeChatPhoneDTO weChatPhone){
        log.info("调用用户登录接口,传递参数：{}", JSONObject.toJSONString(weChatPhone));
        return success(userService.wxlogin(weChatPhone));
    }

    /**
     * 注册用户
     *
     * @param user 用户
     * @return ResultVO
     */
    @PostMapping("register")
    @Operation(summary = "注册用户")
    public ResponseEntity<ResultVO<User>> register(
            @Parameter(description = "用户实体")
            @RequestBody @Valid User user) {
        log.info("调用注册用户接口,传递参数：{}", JSONObject.toJSONString(user));
        return success(userService.register(user));
    }

    @GetMapping("delete/{id}")
    @Operation(summary = "删除用户")
    public ResponseEntity<ResultVO<Void>> deleteUser(@PathVariable @PositiveOrZero int id) {
        userService.deleteByUserId(id);
        return success();
    }

    @PostMapping("update")
    @Operation(summary = "更新用户信息")
    public ResponseEntity<ResultVO<Void>> updateUser(@Parameter(description = "用户实体") @RequestBody @Valid User user) {
        log.info("调用更新用户信息接口,传递参数：{}", JSONObject.toJSONString(user));
        userService.updateUser(user);
        return success();
    }

    @PostMapping("select")
    @Operation(summary = "微信小程序查询用户接口")
    public ResponseEntity<ResultVO<Page<User>>> select(@RequestBody @Valid PageParam<UserConditionDto> conditionDto) {
        log.info("微信小程序查询用户接口,传递参数：{}", JSONObject.toJSONString(conditionDto));
        return success(userService.selectUserByCondition(conditionDto));
    }

    @PostMapping("selectList")
    @Operation(summary = "后台查询用户")
    public ResponseEntity<ResultVO<Page<User>>> selectList(@RequestBody PageParam<User> userPageParam) {
        return success(userService.selectUser(userPageParam));
    }

    @PostMapping("picture")
    @Operation(summary = "查询用户照片")
    public ResponseEntity<ResultVO<List<UserPhoto>>> selectPictureById(@RequestBody UserPhoto userPhoto) {
        log.info("调用查询用户照片接口,传递参数：{}", JSONObject.toJSONString(userPhoto));
        return success(userService.selectPictureById(userPhoto));
    }

    @PostMapping("getUserInfo")
    @Operation(summary = "根据id或者编号查询用户")
    public ResponseEntity<ResultVO<User>> getUserInfo(@RequestBody User user) {
        log.info("调用根据id或者编号查询用户接口,传递参数：{}", JSONObject.toJSONString(user));
        return success(userService.getUserInfo(user));
    }

    @GetMapping("userCount")
    @Operation(summary = "获取用户总数")
    public ResponseEntity<ResultVO<Map<String,Integer>>> getUserCount(@RequestBody @Valid PageParam<UserConditionDto> conditionDto) {
        log.info("调用获取用户总数,传递参数：{}", JSONObject.toJSONString(conditionDto));
        return success(userService.getUserCount(conditionDto));
    }

    //TODO
    //重写微信查看详情方法，每次查看

    @PostMapping("wxGetUserInfo")
    @Operation(summary = "根据id或者编号查询用户")
    public ResponseEntity<ResultVO<User>> wxGetUserInfo(@RequestBody UserConditionDto user) {
        log.info("调用根据id或者编号查询用户接口,传递参数：{}", JSONObject.toJSONString(user));
        return success(userService.wxGetUserInfo(user));
    }

}